/**场景1
 * 计算数组中所有值的和
 * 
 */

//es5
var numbers=[10,20,30];
var sum=0
for(let i=0;i<numbers.length;i++){
    sum=sum+numbers[i];
}
console.log(sum);

//es6
var sumValue=numbers.reduce(function(sum,number){
    return sum+number
},0)//需要两个参数,第一个参数需要给初始值
console.log(sumValue);



 /**场景2
  * 将数组中对象的某个属性抽离到另外一个数组中
  * 
  */
 var primaryColor=[
     {color:"red"},
     {color:"yellow"},
     {color:"blue"}
 ];
 var colors=primaryColor.reduce(function(previous,primaryColor){
    previous.push(primaryColor.color);
    return previous;
 },[]);
 console.log(colors);

 /**场景3
  * 
  * 判断字符串中括号是否对称
  */
 function balancedParents(string){
     return !string.split("").reduce(function(previous,char){
         if(previous<0){return previous;}//一旦出现')('即可判断为false
         if(char =="("){return ++previous;}
         if(char ==")"){return --previous;}
         return previous;
     },0);
 }
 console.log(balancedParents(")(((()))()"));